package game.map.generator;

import java.io.Serializable;
import java.util.Random;

import data.DataManager.BlockType;
import game.CavesList;
import game.graphics.Block;
import helpers.Interpolator;

public class CaveGenerate implements Serializable {
	private Block blocks[][];
	
	/**
	 * TOMUHLE SNAD BUDU ROZUMNET
	 */
	
	private static final int MIN_CAVES_COUNT = 20;
	private static final int MAX_CAVES_COUNT = 32;
	private static final int MIN_CAVES_POSITION_HEIGHT = 700;
	
	/**
	 * SPOJKY DOCELA OVLIVNUJOU DELKU JESKYNE
	 */
	
	private static final int MIN_CAVES_LINKS = 10;
	private static final int MAX_CAVES_LINKS = 14;
	
	/**
	 * ROZPTYL MEZI TEMA SPOJKAMA
	 */
	
	private static final int MIN_CAVES_AMONG_X = 12;
	private static final int MAX_CAVES_AMONG_X = 24;
	
	private static final int MIN_CAVES_HEIGHT	=	10;
	private static final int MAX_CAVES_HEIGHT	=	22;
	
	private static final int MIN_CIRCLE_SIZE	=	3;
	private static final int MAX_CIRCLE_SIZE	=	6;
	
	
	public CaveGenerate(Block blocks[][]){
		Random random = new Random();
		
		this.blocks = blocks;
		
		int cavesCount = random.nextInt(MAX_CAVES_COUNT - MIN_CAVES_COUNT) + MIN_CAVES_COUNT;
		
		
		for(int i = 0; i < cavesCount; i++){
			int startX, startY;
			
			int caveLength;
			int caveAmongX;
			int caveAmongY;
			
			startX		=	random.nextInt(4095);
			startY		=	random.nextInt(1023 - MIN_CAVES_POSITION_HEIGHT) + MIN_CAVES_POSITION_HEIGHT;
			
			if(startX < 20) startX = 20;
			if(startX > 4000) startX = 4000;
			
			CavesList.getInstance().addNewCave(startX, startY);
			
			caveLength	=	random.nextInt(MAX_CAVES_LINKS - MIN_CAVES_LINKS) + MIN_CAVES_LINKS;
			
			caveAmongX	=	random.nextInt(MAX_CAVES_AMONG_X - MIN_CAVES_AMONG_X) + MIN_CAVES_AMONG_X;
			caveAmongY	=	random.nextInt(MAX_CAVES_HEIGHT - MIN_CAVES_HEIGHT) + MIN_CAVES_HEIGHT;
			
			
			/**
			 * Generovani jeskyne
			 */
				int valueX = startX;
			
				int[] valueY = new int[caveLength];
				
				valueY[0] = 0;
				
				for(int j = 1; j < caveLength; j++){
					valueY[j] = random.nextInt(caveAmongY) - (caveAmongY/2);
					
					int yY = startY + valueY[j-1];
					int yY2 = startY + valueY[j];
					
					
					for(int k = 0; k <= caveAmongX+1; k++){
						
						
						int number = (int)Interpolator.CosineInterpolate(k * (1.0 / (caveLength)), yY, yY2);
						
						int xXXxX = valueX+k;
						if(xXXxX < 20) xXXxX = 20;
						if(xXXxX > 4096 - 40) xXXxX = 4096-40;
						
						if(number > 1000) number = 1000;
						
						int velikostKruhu = random.nextInt(MAX_CIRCLE_SIZE - MIN_CIRCLE_SIZE) + MIN_CIRCLE_SIZE;;
						
						generateCircle(xXXxX,number, velikostKruhu);
					}
					
					caveAmongX = random.nextInt(MAX_CAVES_AMONG_X - MIN_CAVES_AMONG_X) + MIN_CAVES_AMONG_X;
					valueX = valueX + caveAmongX;
				}
			
			
			/**
			 * 
			 */
			
		}
	}
	
	public void generateCircle(int blockX, int blockY, int radius){
		int x = blockX*32 + 16;
		int y = blockY*32 + 16;
		
		int noveX,noveY;
		
		for(int i = 0; i < 360; i+=2){
			for(int j = 0; j <= radius; j++){					
					noveX = (int)(Math.cos(Math.toRadians(i)) * ((j)*32));
					noveY = (int)(Math.sin(Math.toRadians(i)) * ((j)*32));
					
					noveX = noveX / 32;
					noveY = noveY / 32;
						
					blocks[blockX+noveX][blockY+noveY].destroy();
			}
		}
	}
}
